//
//  WindowCustom.m
//  ig
//
//  Created by s on 16/6/15.
//  Copyright © 2016年 CMCCIOT. All rights reserved.
//

#import "CMCCIOTIGSharePopWindow.h"

static CMCCIOTIGSharePopWindow * sharePopWindow;

@implementation CMCCIOTIGSharePopWindow{
    BOOL  isChangeView;
}
@synthesize mainView,popView,subRect;

+ (CMCCIOTIGSharePopWindow *)sharedWindow
{
    static dispatch_once_t once;
    dispatch_once(&once, ^{ sharePopWindow = [[self alloc] init];});
    return sharePopWindow;
        
}
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setFrame:[[UIScreen mainScreen]bounds]];
        [self setWindowLevel:UIWindowLevelAlert];
        [self setUserInteractionEnabled:YES];
        mainView = [[UIView alloc] initWithFrame:self.bounds];
        [self addSubview:mainView];
        self.closeType=CMCC_CLOSEDefault;
        
    }
    return self;
}
-(void)showWithSubView:(UIView *)view
{
    
    if (popView) {
       // 如果存在 几次连续调用 先将最先一次移除
       [self changeViewToOtherView:view];
    }
    else{
        [[[UIApplication sharedApplication] keyWindow] endEditing:YES];
        popView=view;
        if (view)
        {
            // 居中
            [view setCenter:CGPointMake(self.frame.size.width / 2, self.frame.size.height / 2)];
            if (view.tag == 0)
            {
                view.tag = 2551;
            }
            [mainView addSubview:view];
             subRect= view.frame;
        }
       
        
        [self  showAnimationView];
       
    }
    
}

-(void)showAnimationView{
    // 设置背景色和透明度
    [self setBackgroundColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0.5]];
    //    self.alpha = 0;
    [self makeKeyWindow];
    self.hidden =NO;
    self.alpha=0.0;
    [UIView animateWithDuration:0.3
                     animations:^{
                         self.alpha = 1.0;
                     } completion:^(BOOL finished) {
                          isChangeView=NO;
                     }];

}


- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    //CGRect fileterRect = _filterView.frame;
    //    NSLog(@"%@",[[touches anyObject] locationInView:self]);
    CGPoint point = [[touches anyObject] locationInView:self];
    //    NSLog(@"%f, %f", point.x, point.y);
    
    // 如果触碰点在空白处，就取消掉弹出框
    if (![self CGPoint:point isInRectZone:subRect]) {
    // 如果关闭类型为默认
        if (self.closeType==CMCC_CLOSEDefault) {
            [self dismiss];

        }
    }
}
- (BOOL)CGPoint:(CGPoint)point isInRectZone:(CGRect)zone
{
    // 如果在指定区域内返回YES
    if (point.x > zone.origin.x &&
        point.x < zone.origin.x + zone.size.width &&
        point.y > zone.origin.y &&
        point.y < zone.origin.y + zone.size.height) {
        return YES;
    }
    // 如果在区域外返回NO
    else
        return NO;
}

- (void)dismiss
{
    if (self.closeType==CMCC_CLOSE_NOTHING) {
        return;
    }
    
    self.closeType=CMCC_CLOSEDefault;
   
    self.alpha=1.0;
    [UIView animateWithDuration:0.0
                     animations:^{
                         self.alpha = 0.0;
                     } completion:^(BOOL finished) {
                         [self resignKeyWindow];
                         self.hidden = YES ;
                         if (isChangeView) {
                             [self  showAnimationView];
                         }
                         else{
                         
                             //   移除popView的通知
                             [[NSNotificationCenter defaultCenter]  removeObserver:popView];
                             [popView removeFromSuperview];
                             popView=nil;
                            

                         }
                             
                         }
                     ];
    
    
    
}

- (void)changeViewToOtherView:(UIView *)otherView
{
        CGRect otherRect = CGRectMake(0, 0, otherView.frame.size.width, otherView.frame.size.height);
        //   移除popView的通知
        [[NSNotificationCenter defaultCenter]  removeObserver:popView];
        
        // 移出原来的view
        [popView removeFromSuperview];
         popView = nil;
        
        // 重新调整显示view大小
        [otherView setFrame:CGRectMake((self.frame.size.width - otherView.frame.size.width) / 2,
                                     (self.frame.size.height - otherView.frame.size.height) / 2,
                                     otherRect.size.width,
                                     otherRect.size.height)];
        
        popView=otherView;
        [mainView addSubview:otherView];
        subRect= otherView.frame;
        isChangeView=YES;
    
//        [UIView animateWithDuration:0.3f animations:^{
//            // 使原来的view消失
//            for (UIView *subView in popView.subviews) {
//                subView.alpha = 0;
//            }
//           
//        } completion:^(BOOL finished) {
//            
//            
//            // 显示新的view
//            [self showWithSubView:otherView];
//        }];
  
}

-(void)addSubview:(UIView *)view{

    [super addSubview:view];
    if (!self.popView) {
        self.alpha=1.0;
        self.hidden=NO;
        self.backgroundColor=[UIColor  clearColor];
    }
    else{
        self.userInteractionEnabled=YES;
    }
}

-(void)willRemoveSubview:(UIView *)subview{
    [super willRemoveSubview:subview];
    if (!self.popView){
        self.alpha=0.0;
        self.hidden=YES;
        self.backgroundColor=[UIColor  clearColor];

    }

}


@end
